package com.alkaid.user.service.impl;

import com.alkaid.commons.core.CacheKeyDefinition;
import com.alkaid.commons.msg.MsgDefinition;
import com.alkaid.commons.utils.JsonResult;
import com.alkaid.commons.utils.JwtUtils;
import com.alkaid.user.mapper.AuthorityMapper;
import com.alkaid.user.mapper.UserMapper;
import com.alkaid.user.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author Kylin
 */
@Service
public class UserDetailsImpl implements UserDetailsService {

    private UserMapper userMapper;

    private AuthorityMapper authorityMapper;

    private RedisUtils redisUtils;

    private PasswordEncoder passwordEncoder;
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    @Autowired
    public void setAuthorityMapper(AuthorityMapper authorityMapper) {
        this.authorityMapper = authorityMapper;
    }
    @Autowired
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }
    @Autowired
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    private static JsonResult<Void> result;

    public static JsonResult<Void> getResult() {
        return result;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("------loadUserByUsername------");
        String[] str = username.split("-");
        String grantType = str[0];
        username = str[1];
        System.out.println(grantType + "------" + username);
        if ("".equals(username)) {
            // 账号为空
            System.err.println(MsgDefinition.USERNAME_NULL);
            result = new JsonResult<>(404, null, MsgDefinition.USERNAME_NULL);
            throw new UsernameNotFoundException(MsgDefinition.USERNAME_NULL);
        }
        com.alkaid.commons.entity.User userDO = userMapper.findUserByUsername(username);
        if (userDO == null) {
            // 用户未注册
            System.err.println(MsgDefinition.USER_NOT_REG);
            result = new JsonResult<>(404, null, MsgDefinition.USER_NOT_REG);
            throw new UsernameNotFoundException(MsgDefinition.USER_NOT_REG);
        }
        String password = userDO.getPassword();
        // 手机验证码方式登录
        if ("mobile".equals(grantType)) {
            String pwd = (String) redisUtils.getFromRedis(CacheKeyDefinition.SMS_KEY_MAP.get("login") + username);
            if (pwd == null) {
                // 验证码未发送
                System.err.println(MsgDefinition.SMS_NOLL);
                result = new JsonResult<>(404, null, MsgDefinition.SMS_NOLL);
                throw new UsernameNotFoundException(MsgDefinition.SMS_NOLL);
            }
            password = passwordEncoder.encode(pwd);
        }
        return new User(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList(
                JwtUtils.parseAuthorities(authorityMapper.findAuthorityByUserId(userDO.getId()))));
    }
}
